﻿@inject IDataCacheService _dataCacheService
@inject NavigationManager NavigationManager
@inject IHttpService _httpService


<div>
    @if (_dataCacheService.CVThematicPageCache != null)
    {
        <div class="row">
            @if (_dataCacheService.CVThematicPageCache.News.Any() && _dataCacheService.IsApp == false)
            {
                <div class="@("col-12 mb-4 "+(_dataCacheService.CVThematicPageCache.Carousels.Any()?"col-md-5":""))">
                    <CnGalWebSite.Shared.MasaComponent.PC.Home.ThematicPages.Cards.NewsCard Model="_dataCacheService.CVThematicPageCache.News" />

                </div>
            }
            @if (_dataCacheService.CVThematicPageCache.Carousels.Any())
            {
                <div class="@("col-12 mb-4 "+(_dataCacheService.CVThematicPageCache.News.Any()&&_dataCacheService.IsApp==false?"col-md-7":""))">
                    <CnGalWebSite.Shared.MasaComponent.PC.Home.ThematicPages.Cards.CarouselCard Model="_dataCacheService.CVThematicPageCache.Carousels" />
                </div>
            }
            <div class="col-12">
                <CnGalWebSite.Shared.MasaComponent.PC.Home.ThematicPages.Cards.TagsCard Model="_dataCacheService.CVThematicPageCache" OnChanged="()=>OnChanged()" />
            </div>
            <div class="col-12">
                <CnGalWebSite.Shared.MasaComponent.PC.Home.ThematicPages.Cards.SearchCard OnChanged="()=>OnChanged()" />
            </div>

            @foreach (var item in Model.Skip((_dataCacheService.CVThematicPageCache.CurrentPage - 1) * MaxCount).Take(MaxCount))
            {

                <div class="col-4 col-md-3 col-lg-2 mb-4">
                    <CnGalWebSite.Shared.MasaComponent.PC.Home.ThematicPages.Cards.CVInforCard Model="item" />
                </div>
            }


        </div>
        @if (TotalPages > 1)
        {

            <CnGalWebSite.Components.Paginations.MasaPagination Value="@_dataCacheService.CVThematicPageCache.CurrentPage" Length="@TotalPages" ValueChanged="OnPageClick" />


        }

    }
    else
    {
        <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />
    }

</div>


@code {

    [Parameter]
    public List<CVInforViewModel> Model { get; set; } = new List<CVInforViewModel>();

    public int MaxCount
    {
        get
        {
            return 18;
        }
    }

    public int TotalPages => ((Model.Count - 1) / MaxCount) + 1;


    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    protected override async Task OnInitializedAsync()
    {
        try
        {
            if (_dataCacheService.CVThematicPageCache == null)
            {
                _dataCacheService.CVThematicPageCache = await _httpService.GetAsync<CVThematicPageViewModel>("api/tags/GetCVThematicPageViewModel");
            }

            OnChanged(true);

        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "无法获取内容中心视图数据");
        }

    }

    public void OnChanged(bool isInit = false)
    {
        Model.Clear();
        Model.AddRange(_dataCacheService.CVThematicPageCache.CVInfors);

        if (_dataCacheService.CVThematicPageCache == null)
        {

            
        }

        //先按选中的标签筛除
        if (_dataCacheService.CVThematicPageCache.SelectedTags.Any())
        {
            foreach (var item in _dataCacheService.CVThematicPageCache.Tags)
            {
                var tags = item.Children.Where(s => _dataCacheService.CVThematicPageCache.SelectedTags.Contains(s.Title));
                if (tags.Any())
                {
                    Model.RemoveAll(s => s.Tags.Any(s => tags.Select(s => s.Title).Contains(s.Name)) == false);
                }
            }
        }


        //筛除有分类的
        if (_dataCacheService.CVThematicPageCache.UnlabeledTags.Any())
        {
            foreach (var item in _dataCacheService.CVThematicPageCache.UnlabeledTags)
            {
                var tags = _dataCacheService.CVThematicPageCache.Tags.FirstOrDefault(s => _dataCacheService.CVThematicPageCache.UnlabeledTags.Contains(s.Title)).Children;
                if (tags.Any())
                {
                    Model.RemoveAll(s => s.Tags.Any(s => tags.Select(s => s.Title).Contains(s.Name)));
                }
            }
        }

        //搜索
        if (string.IsNullOrWhiteSpace(_dataCacheService.CVThematicPageCache.SearchString) == false)
        {
            Model.RemoveAll(s => s.Infor.Name.ToUpper().Contains(_dataCacheService.CVThematicPageCache.SearchString.ToUpper()) == false && _dataCacheService.CVThematicPageCache.SearchString.ToUpper().Contains(s.Infor.Name.ToUpper()) == false);
        }

        //排序
        Model = _dataCacheService.CVThematicPageCache.SortType switch
        {
            CVThematicPageSortType.Random => Model.Random(),
            CVThematicPageSortType.None => Model.OrderByDescending(s => s.LastUploadAudioTime).ThenByDescending(s=>s.IsCertificated).ThenBy(s => s.Infor.MainImage).ToList(),
            CVThematicPageSortType.ReadCount => Model.OrderByDescending(s => s.Infor.ReaderCount).ToList(),
            CVThematicPageSortType.WorkCount => Model.OrderByDescending(s => s.WorkCount).ToList(),
            CVThematicPageSortType.RecentlyPublish => Model.OrderByDescending(s => s.LastPublishTime).ToList(),
            _ => Model
        };

        //重置页数
        if (isInit == false)
        {
            _dataCacheService.CVThematicPageCache.CurrentPage = 1;
        }

        StateHasChanged();

        
    }

    private void OnPageClick(int pageIndex)
    {
        _dataCacheService.CVThematicPageCache.CurrentPage = pageIndex;

        StateHasChanged();

        
    }

}
